package hibernatelogic;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import orm.Users;
import utils.HibernateUtil;

/**
 * 
 * The DAO class for table USERS
 * 
 * @author Igor Petrov
 * 
 */
public class HibUsers {

	public HibUsers() {
	}

	/**
	 * select * from users;
	 * @throws SQLException
	 */
	public Collection getAllUsers() throws SQLException {
		Session session = null;
		List users = new ArrayList<Users>();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			users = session.createCriteria(Users.class).list();
		} catch (Exception e) {
			loggerHibUsers.error("getAllUsers failed!!!", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return users;
	}

	/**
	 * insert into users values([user])
	 * @param user
	 * @throws SQLException
	 */
	public void addUser(Users user) throws SQLException {
		session = null;
		try {
			session = hibUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
		} catch (Exception e) {
			loggerHibUsers.error("addUser failed!!!", e);
			session.getTransaction().rollback();
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	/**
	 * delete from users where ['user' = user]
	 * @param user
	 * @throws SQLException
	 */
	public void deleteUser(Users user) throws SQLException {
		try {
			session = null;
			session = hibUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(user);
			session.getTransaction().commit();
		} catch (Exception e) {
			loggerHibUsers.error("deleteUser failed!", e);
			session.getTransaction().rollback();
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	/**
	 * select * from users where user_id = 'user_id'
	 * @param user_id
	 * @return
	 * @throws SQLException
	 */
	public Users getUserByID(int user_id) throws SQLException {
		session = null;
		Users user = null;
		try {
			session = hibUtil.getSessionFactory().openSession();
			session.beginTransaction();
			user = (Users) session.get(Users.class, user_id);
		} catch (Exception e) {
			loggerHibUsers.error("getUserByID failed", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return user;
	}
	
	/**
	 * user with 'login' and 'password' exists in DB?
	 * @param login
	 * @param password
	 * @return
	 * @throws SQLException
	 */
	public int userExists(String login, String password) throws SQLException {
		int id = 0;
		session = null;
		try {
			session = hibUtil.getSessionFactory().openSession();
			Query query = session.createQuery("from Users where login = :LogIN").setString("LogIN", login);
			query.setMaxResults(1);
			if(query.iterate().hasNext()){
				Users user = (Users) query.uniqueResult();
				String testLog = user.getLogin();
				String testPass = user.getPassword();
				if(login.equals(testLog) && password.equals(testPass)) {
					id = user.getUser_id();
				}
			}
		} catch (Exception e) {
			loggerHibUsers.error("userExists failed!!!", e);
		} finally {
			if(session != null && session.isOpen()){
				session.close();
			}
		}
		return id;
	}

	HibernateUtil hibUtil = new HibernateUtil();
	private Session session = null;
	private static final Logger loggerHibUsers = Logger
			.getLogger(HibUsers.class);
}
